<HTML>
<HEAD>
<TITLE>
BasicType.h
</TITLE>
</HEAD>
<BODY>
<PRE>
<font color="green">/*
* Copyright 2006 the original author or authors.
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*      http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/</font>

<font color="blue">#ifndef</font> AUTUMN_BASICTYPE_H
<font color="blue">#define</font> AUTUMN_BASICTYPE_H

<font color="blue">#include</font> <font color="maroon">"Basic.h"</font>
<font color="blue">#include</font> <font color="maroon">"IAutumnType.h"</font>
<font color="blue">#include</font> <font color="maroon">"AutumnException.h"</font>

<font color="blue">using</font> <font color="blue">namespace</font> std;

<font color="blue">namespace</font> Autumn<font color="black">{</font>

<font color="green">/** 
 * Create correctly value and free it.
 * class T: the true type of value, it must support evaluation operator(=); 
 * createfun: the function that conversion a string to a T value;
 * freefun: the function that free the space created in createfun, for example,
 * T is a pointer to a space created in createfun;
 * 
 * @version 0.1.0
 * @since 2006-11-25
 */</font>
<font color="blue">template</font><font color="black">&#60;</font><font color="blue">class</font> T, T createfun<font color="black">(</font><font color="blue">const</font> <font color="blue">char</font><font color="black">*</font><font color="black">)</font>, <font color="blue">void</font> freefun<font color="black">(</font>T<font color="black">)</font><font color="black">&#62;</font><font color="blue">class</font> BasicType<font color="black">:</font><font color="blue">public</font> IAutumnType<font color="black">{</font>
<font color="blue">private</font><font color="black">:</font>
    string typeFormat;

<font color="blue">public</font><font color="black">:</font>
    BasicType<font color="black">(</font><font color="blue">const</font> string<font color="black">&</font> <font color="blue">name</font><font color="black">)</font><font color="black">:</font> typeFormat<font color="black">(</font><font color="blue">name</font><font color="black">)</font><font color="black">{</font><font color="black">}</font>
    
    <font color="green">/** 
     * Create a value from StrValueList(from it's first element in fact).
     * @param vl A Vector&#60;string&#62;
     * @param it A iterator pointing to the first unused string, it will be changed
     * in this function.
     * @return A pointer to a value of type T
     */</font>
    virtual <font color="blue">void</font><font color="black">*</font> createValue<font color="black">(</font><font color="blue">const</font> string<font color="black">&</font> type, <font color="blue">const</font> StrValueList<font color="black">&</font> vl,
                              StrIterator<font color="black">&</font> it<font color="black">)</font><font color="blue">const</font>;

    <font color="green">/** Free the space where p points */</font>
    virtual <font color="blue">void</font> freeValue<font color="black">(</font><font color="blue">void</font><font color="black">*</font> pp, <font color="blue">const</font> string<font color="black">&</font> type<font color="black">)</font><font color="black">{</font>
        <font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>freeSelfSpace<font color="black">(</font>pp<font color="black">)</font>;
    <font color="black">}</font>
    
    <font color="green">/** Free the space where p points, don't free it's memeber's space */</font>
    virtual <font color="blue">void</font> freeSelfSpace<font color="black">(</font><font color="blue">void</font><font color="black">*</font> p<font color="black">)</font>;

    virtual <font color="blue">bool</font> isThisType<font color="black">(</font><font color="blue">const</font> string<font color="black">&</font> type<font color="black">)</font> <font color="blue">const</font> <font color="black">{</font>
        <font color="blue">return</font> type.compare<font color="black">(</font><font color="blue">this</font><font color="black">-</font><font color="black">&#62;</font>typeFormat<font color="black">)</font> <font color="black">=</font><font color="black">=</font> <font color="maroon">0</font>;
    <font color="black">}</font>
<font color="black">}</font>;

<font color="blue">template</font><font color="black">&#60;</font><font color="blue">class</font> T, T createfun<font color="black">(</font><font color="blue">const</font> <font color="blue">char</font><font color="black">*</font><font color="black">)</font>, <font color="blue">void</font> freefun<font color="black">(</font>T<font color="black">)</font><font color="black">&#62;</font>
<font color="blue">void</font><font color="black">*</font> BasicType<font color="black">&#60;</font>T, createfun, freefun<font color="black">&#62;</font><font color="black">:</font><font color="black">:</font>createValue<font color="black">(</font><font color="blue">const</font> string<font color="black">&</font> type, <font color="blue">const</font> StrValueList<font color="black">&</font> vl, StrIterator<font color="black">&</font> it<font color="black">)</font><font color="blue">const</font>
<font color="black">{</font>
    <font color="blue">if</font><font color="black">(</font> it <font color="black">!</font><font color="black">=</font> vl.end<font color="black">(</font><font color="black">)</font> <font color="black">)</font><font color="black">{</font>
        T <font color="black">*</font>p <font color="black">=</font> <font color="blue">new</font> T;
        <font color="black">*</font>p <font color="black">=</font> createfun<font color="black">(</font><font color="black">(</font><font color="black">*</font>it<font color="black">+</font><font color="black">+</font><font color="black">)</font>.c_str<font color="black">(</font><font color="black">)</font><font color="black">)</font>;
        <font color="blue">return</font> <font color="black">(</font><font color="blue">void</font><font color="black">*</font><font color="black">)</font>p; <font color="green">//*p is the value</font>
    <font color="black">}</font>
    <font color="blue">throw</font> NonValueEx<font color="black">(</font><font color="maroon">"BasicType"</font>, <font color="maroon">"createValue"</font>,
        <font color="maroon">"There is no string in vector!"</font><font color="black">)</font>;
<font color="black">}</font>

<font color="blue">template</font><font color="black">&#60;</font><font color="blue">class</font> T, T createfun<font color="black">(</font><font color="blue">const</font> <font color="blue">char</font><font color="black">*</font><font color="black">)</font>, <font color="blue">void</font> freefun<font color="black">(</font>T<font color="black">)</font><font color="black">&#62;</font>
<font color="blue">void</font> BasicType<font color="black">&#60;</font>T, createfun, freefun<font color="black">&#62;</font><font color="black">:</font><font color="black">:</font>freeSelfSpace<font color="black">(</font><font color="blue">void</font><font color="black">*</font> pp<font color="black">)</font><font color="black">{</font>
    T<font color="black">*</font> p <font color="black">=</font> <font color="black">(</font>T<font color="black">*</font><font color="black">)</font>pp;
    <font color="green">// free space for T if necessary</font>
    <font color="blue">if</font><font color="black">(</font> freefun <font color="black">!</font><font color="black">=</font> NULL<font color="black">)</font><font color="black">{</font>
        freefun<font color="black">(</font><font color="black">*</font>p<font color="black">)</font>;
    <font color="black">}</font>
    
    <font color="green">// free space of T</font>
    <font color="blue">delete</font> p;
<font color="black">}</font>

<font color="black">}</font> <font color="green">// End namespace Autumn</font>

<font color="blue">#endif</font>

</PRE>
</BODY>
</HTML>
